/*     */ package com.zimbra.cs.dav.resource;
/*     */ 
/*     */ import com.google.common.collect.Lists;
/*     */ import com.google.common.io.Closeables;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.HttpUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.dav.DavContext;
/*     */ import com.zimbra.cs.dav.DavContext.RequestProp;
/*     */ import com.zimbra.cs.dav.DavElements;
/*     */ import com.zimbra.cs.dav.DavException;
/*     */ import com.zimbra.cs.dav.caldav.Range.TimeRange;
/*     */ import com.zimbra.cs.dav.property.CalDavProperty;
/*     */ import com.zimbra.cs.index.MessageHit;
/*     */ import com.zimbra.cs.index.SortBy;
/*     */ import com.zimbra.cs.index.ZimbraHit;
/*     */ import com.zimbra.cs.index.ZimbraQueryResults;
/*     */ import com.zimbra.cs.mailbox.Flag;
/*     */ import com.zimbra.cs.mailbox.Flag.FlagInfo;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxIndex;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailbox.Message.CalendarItemInfo;
/*     */ import com.zimbra.cs.mailbox.Mountpoint;
/*     */ import com.zimbra.cs.mailbox.calendar.Invite;
/*     */ import java.io.IOException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collection;
/*     */ import java.util.Collections;
/*     */ import java.util.EnumSet;
/*     */ import java.util.HashMap;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Set;
/*     */ import org.dom4j.Element;
/*     */ import org.dom4j.QName;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ScheduleInbox
/*     */   extends CalendarCollection
/*     */ {
/*     */   public ScheduleInbox(DavContext ctxt, Folder f)
/*     */     throws DavException, ServiceException
/*     */   {
/*  58 */     super(ctxt, f);
/*  59 */     addResourceType(DavElements.E_SCHEDULE_INBOX);
/*  60 */     String user = getOwner();
/*  61 */     addProperty(CalDavProperty.getCalendarFreeBusySet(user, getCalendarFolders(ctxt)));
/*     */   }
/*     */   
/*  64 */   private QName[] supportedInboxReports = null;
/*     */   
/*     */   protected QName[] getSupportedReports()
/*     */   {
/*  68 */     if (this.supportedInboxReports == null) {
/*  69 */       List<QName> reportList = Lists.newArrayList(super.getSupportedReports());
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  75 */       reportList.remove(DavElements.E_FREE_BUSY_QUERY);
/*  76 */       this.supportedInboxReports = ((QName[])reportList.toArray(new QName[reportList.size()]));
/*     */     }
/*  78 */     return this.supportedInboxReports;
/*     */   }
/*     */   
/*     */   public Collection<DavResource> getChildren(DavContext ctxt, Range.TimeRange tr) throws DavException
/*     */   {
/*     */     try {
/*  84 */       return getAppointmentsByUids(ctxt, null);
/*     */     } catch (ServiceException se) {
/*  86 */       ZimbraLog.dav.error("can't get schedule messages in folder " + getId(), se); }
/*  87 */     return Collections.emptyList();
/*     */   }
/*     */   
/*     */ 
/*  91 */   protected static final Set<MailItem.Type> SEARCH_TYPES = EnumSet.of(MailItem.Type.MESSAGE);
/*     */   
/*     */   public Collection<DavResource> getAppointmentsByUids(DavContext ctxt, List<String> hrefs) throws ServiceException, DavException
/*     */   {
/*  95 */     List<DavResource> result = new ArrayList();
/*  96 */     if (!DavResource.isSchedulingEnabled()) {
/*  97 */       return result;
/*     */     }
/*  99 */     Account target = null;
/* 100 */     Provisioning prov = Provisioning.getInstance();
/* 101 */     if (ctxt.getActingAsDelegateFor() != null) {
/* 102 */       target = prov.getAccountByName(ctxt.getActingAsDelegateFor());
/*     */     }
/* 104 */     String query = "is:invite is:unread inid:" + getId() + " after:\"-1month\" ";
/* 105 */     Mailbox mbox = getMailbox(ctxt);
/* 106 */     ZimbraQueryResults zqr = null;
/*     */     try {
/* 108 */       zqr = mbox.index.search(ctxt.getOperationContext(), query, SEARCH_TYPES, SortBy.DATE_ASC, 100);
/* 109 */       while (zqr.hasNext()) {
/* 110 */         ZimbraHit hit = zqr.getNext();
/* 111 */         if ((hit instanceof MessageHit)) {
/* 112 */           Message msg = ((MessageHit)hit).getMessage();
/* 113 */           if (((target != null) || (msg.getCalendarIntendedFor() == null)) && 
/*     */           
/*     */ 
/* 116 */             (msg.isInvite()) && (msg.hasCalendarItemInfos()) && 
/*     */             
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 123 */             (!"REPLY".equals(msg.getCalendarItemInfo(0).getInvite().getMethod())))
/*     */           {
/*     */ 
/* 126 */             if (target != null) {
/* 127 */               if (msg.getCalendarIntendedFor() != null)
/*     */               {
/*     */ 
/* 130 */                 Account apptRcpt = prov.getAccountByName(msg.getCalendarIntendedFor());
/* 131 */                 if ((apptRcpt == null) || (!apptRcpt.getId().equals(target.getId()))) {}
/*     */               }
/*     */             }
/*     */             else {
/* 135 */               DavResource rs = UrlNamespace.getResourceFromMailItem(ctxt, msg);
/* 136 */               if (rs != null) {
/* 137 */                 String href = UrlNamespace.getRawResourceUrl(rs);
/* 138 */                 if (hrefs == null) {
/* 139 */                   result.add(rs);
/*     */                 } else {
/* 141 */                   boolean found = false;
/* 142 */                   for (String ref : hrefs) {
/* 143 */                     if (HttpUtil.urlUnescape(ref).equals(href)) {
/* 144 */                       result.add(rs);
/* 145 */                       found = true;
/* 146 */                       break;
/*     */                     }
/*     */                   }
/* 149 */                   if (!found)
/* 150 */                     result.add(new DavResource.InvalidResource(href, getOwner()));
/*     */                 }
/*     */               }
/*     */             } }
/*     */         }
/*     */       }
/* 156 */     } catch (Exception e) { ZimbraLog.dav.error("can't search: uri=" + getUri(), e);
/*     */     } finally {
/* 158 */       Closeables.closeQuietly(zqr);
/*     */     }
/* 160 */     return result;
/*     */   }
/*     */   
/*     */   public void patchProperties(DavContext ctxt, Collection<Element> set, Collection<QName> remove) throws DavException, IOException
/*     */   {
/* 165 */     ArrayList<Element> newSet = null;
/* 166 */     for (Element el : set) {
/* 167 */       if (el.getQName().equals(DavElements.E_CALENDAR_FREE_BUSY_SET)) {
/* 168 */         Iterator<?> hrefs = el.elementIterator(DavElements.E_HREF);
/* 169 */         ArrayList<String> urls = new ArrayList();
/* 170 */         while (hrefs.hasNext())
/* 171 */           urls.add(((Element)hrefs.next()).getText());
/*     */         try {
/* 173 */           updateCalendarFreeBusySet(ctxt, urls);
/*     */         } catch (ServiceException e) {
/* 175 */           ctxt.getResponseProp().addPropError(DavElements.E_CALENDAR_FREE_BUSY_SET, new DavException("error", 424));
/*     */         } catch (DavException e) {
/* 177 */           ctxt.getResponseProp().addPropError(DavElements.E_CALENDAR_FREE_BUSY_SET, e);
/*     */         }
/* 179 */         if (newSet == null) {
/* 180 */           newSet = new ArrayList(set);
/*     */         }
/* 182 */         newSet.remove(el);
/*     */       }
/*     */     }
/* 185 */     if (newSet != null)
/* 186 */       set = newSet;
/* 187 */     super.patchProperties(ctxt, set, remove);
/*     */   }
/*     */   
/*     */   private void updateCalendarFreeBusySet(DavContext ctxt, ArrayList<String> urls) throws ServiceException, DavException {
/* 191 */     String prefix = "/dav/" + getOwner();
/* 192 */     Mailbox mbox = getMailbox(ctxt);
/* 193 */     HashMap<String, Folder> folders = new HashMap();
/* 194 */     for (Folder f : getCalendarFolders(ctxt)) {
/* 195 */       folders.put(f.getPath(), f);
/*     */     }
/* 197 */     for (String origurl : urls)
/*     */     {
/* 199 */       String url = HttpUtil.urlUnescape(origurl);
/* 200 */       if (url.startsWith(prefix))
/*     */       {
/*     */ 
/* 203 */         String path = url.substring(prefix.length());
/* 204 */         if (path.endsWith("/"))
/* 205 */           path = path.substring(0, path.length() - 1);
/* 206 */         Folder f = (Folder)folders.remove(path);
/* 207 */         if (f == null)
/*     */         {
/* 209 */           DavResource rs = UrlNamespace.checkRenamedResource(getOwner(), path);
/* 210 */           if ((rs == null) || (!rs.isCollection()))
/* 211 */             throw new DavException("folder not found " + url, 424);
/* 212 */           f = mbox.getFolderById(ctxt.getOperationContext(), ((MailItemResource)rs).getId());
/*     */         }
/* 214 */         if ((f.getFlagBitmask() & Flag.BITMASK_EXCLUDE_FREEBUSY) != 0)
/*     */         {
/* 216 */           ZimbraLog.dav.debug("clearing EXCLUDE_FREEBUSY for " + path);
/* 217 */           mbox.alterTag(ctxt.getOperationContext(), f.getId(), MailItem.Type.FOLDER, Flag.FlagInfo.EXCLUDE_FREEBUSY, false, null);
/*     */         } } }
/* 219 */     if (!folders.isEmpty()) {
/* 220 */       for (Folder f : folders.values()) {
/* 221 */         ZimbraLog.dav.debug("setting EXCLUDE_FREEBUSY for " + f.getPath());
/* 222 */         mbox.alterTag(ctxt.getOperationContext(), f.getId(), MailItem.Type.FOLDER, Flag.FlagInfo.EXCLUDE_FREEBUSY, true, null);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private Collection<Folder> getCalendarFolders(DavContext ctxt) throws ServiceException, DavException {
/* 228 */     ArrayList<Folder> calendars = new ArrayList();
/* 229 */     Mailbox mbox = getMailbox(ctxt);
/* 230 */     for (Folder f : mbox.getFolderList(ctxt.getOperationContext(), SortBy.NONE)) {
/* 231 */       if ((!(f instanceof Mountpoint)) && ((f.getDefaultView() == MailItem.Type.APPOINTMENT) || (f.getDefaultView() == MailItem.Type.TASK)))
/*     */       {
/* 233 */         calendars.add(f);
/*     */       }
/*     */     }
/* 236 */     return calendars;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/resource/ScheduleInbox.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */